#!/bin/bash  
# 定义变量，MySQL配置信息  
DB_USER="your_mysql_username"  
DB_PASS="your_mysql_password"  
DB_NAME="your_database_name"  
DB_HOST="localhost"  
BACKUP_DIR="/path/to/backup/directory"  
BACKUP_FILE="${DB_NAME}_$(date +%Y%m%d%H%M%S).sql"  
  
# 检查备份目录是否存在，不存在则创建  
mkdir -p $BACKUP_DIR  
  
# 使用mysqldump备份数据库  
mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$BACKUP_FILE  
  
# 检查备份是否成功  
if [ $? -eq 0 ]; then  
    echo "MySQL database backup completed successfully: $BACKUP_DIR/$BACKUP_FILE"  
else  
    echo "MySQL database backup failed!"  
fi
